package com.sychip.fhc.app.wsn.common.util

import com.sychip.fhc.app.wsn.screen.MapDeviceDto

/**
 * 对象在容器中寻找合适位置
 *
 * @property containerWidth  容器宽度
 * @property containerHeight  容器高度
 * @property devW  对象高度
 * @property devH  对象宽度
 * @property gapW  对象间横向间隔
 * @property gapH  对象间纵向间隔
 * @property gridSize  碰撞时起步间隔
 * @constructor Create empty Device layout helper
 */
class DeviceLayoutHelper(
    private val containerWidth: Int = 1000,
    private val containerHeight: Int = 1000,
    private val devW: Int = 100,
    private val devH: Int = 100,
    private val gapW: Int = 300,
    private val gapH: Int = 100,
    private val gridSize: Int = 20
) {
    /**
     * Position
     *
     * @property x
     * @property y
     * @constructor Create empty Position
     */
    data class Position(val x: Float, val y: Float)

    /**
     * Layout devices
     *
     * @param devices
     * @return
     */
    fun layoutDevices(devices: MutableList<MapDeviceDto>): MutableList<MapDeviceDto> {
        val result = devices.toMutableList()
        val occupiedPositions = mutableSetOf<Position>()

        // 记录所有有效位置
        devices.filter { it.offsetX >= 0 && it.offsetY >= 0 }
            .forEach { occupiedPositions.add(Position(it.offsetX, it.offsetY)) }

        var currentX = 20f
        var currentY = 20f

        result.forEachIndexed { index, device ->
            if (device.offsetX < 0 || device.offsetY < 0) {
                var found = false
                while (!found && currentY <= containerHeight - devH) {
                    while (currentX <= containerWidth - devW) {
                        val newPos = Position(currentX, currentY)
                        if (!isOverlapping(newPos, occupiedPositions)) {
                            result[index].offsetX = currentX
                            result[index].offsetY = currentY
                            occupiedPositions.add(newPos)
                            found = true
                            currentX += devW + gapW
                            break
                        }
                        currentX += gridSize.toFloat()
                    }
                    if (!found) {
                        currentX = 20f
                        currentY += devH + gapH  // 使用devH+gapH作为垂直步长
                    }
                }
            } else {
                occupiedPositions.add(Position(device.offsetX, device.offsetY))
            }
        }
        return result
    }

    /**
     * Is overlapping
     *
     * @param pos
     * @param occupied
     * @return
     */
    private fun isOverlapping(pos: Position, occupied: Set<Position>): Boolean {
        // 考虑间距后的碰撞检测
        val right = pos.x + devW + gapW
        val bottom = pos.y + devH + gapH

        return occupied.any { existing ->
            val existingRight = existing.x + devW + gapW
            val existingBottom = existing.y + devH + gapH
            pos.x < existingRight && right > existing.x &&
                    pos.y < existingBottom && bottom > existing.y
        }
    }
}